package info.androidhive.tabsswipe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import info.androidhive.tabsswipe.R;

import java.util.ArrayList;

public class ExpListAdapter extends BaseExpandableListAdapter {

    private ArrayList<ArrayList<String>> mGroups;
    private Context mContext;

    String first_card_data[] = new String[]{
            "История операций",
            "операция_1 16.06.14 22:38",
            "операция_2 16.06.14 22:38",
            "операция_3 16.06.14 22:38",
            "операция_4 16.06.14 22:38",
            "операция_5 16.06.14 22:38",
            "операция_6 16.06.14 22:38",
            "операция_7 16.06.14 22:38",
            "операция_8 16.06.14 22:38",
            "операция_9 16.06.14 22:38",
            "операция_10 16.06.14 22:38"

    };
    String second_card_data[] = new String[]{
            "История операций",
            "операция_1 16.06.14 22:58"

    };
    String third_card_data[] = new String[]{
            "История операций",
            "операция_1 10.04.14 14:36"

    };

    public ExpListAdapter(Context context, ArrayList<ArrayList<String>> groups) {
        mContext = context;
        mGroups = groups;
    }

    @Override
    public int getGroupCount() {
        return mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mGroups.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mGroups.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {
        BankHolder holder = null;


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_view, null);
            holder = new BankHolder();
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
            holder.textGroup1 = (TextView) convertView.findViewById(R.id.textGroup1);
            holder.textGroup2 = (TextView) convertView.findViewById(R.id.textGroup2);
            holder.textGroup3 = (TextView) convertView.findViewById(R.id.textGroup3);
            holder.textGroup4 = (TextView) convertView.findViewById(R.id.textGroup4);

            convertView.setTag(holder);
        } else {
            holder = (BankHolder) convertView.getTag();
        }
        switch (groupPosition) {
            case 0: {
                holder.imgIcon.setImageResource(R.drawable.privat);
                holder.textGroup1.setText("Work");
                holder.textGroup2.setText("ПриватБанк");
                holder.textGroup3.setText("1700.50");
                holder.textGroup4.setText("15.06.14 14:29");
                return convertView;
            }

            case 1: {
                holder.imgIcon.setImageResource(R.drawable.aval);
                holder.textGroup1.setText("Student");
                holder.textGroup2.setText("Аваль");
                holder.textGroup3.setText("700.50");
                holder.textGroup4.setText("15.06.14 15:45");
                return convertView;
            }

            case 2: {
                holder.imgIcon.setImageResource(R.drawable.exim_bank);
                holder.textGroup1.setText("Credit");
                holder.textGroup2.setText("ЭксимБанк");
                holder.textGroup3.setText("12700.50");
                holder.textGroup4.setText("15.06.14 22:30");
                return convertView;
            }

        }


        if (isExpanded) {
            //Изменяем что-нибудь, если текущая Group раскрыта
        } else {
            //Изменяем что-нибудь, если текущая Group скрыта
        }

        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_view, null);
        }

        TextView textChild = (TextView) convertView.findViewById(R.id.textView);
        if (groupPosition == 0) {
            textChild.setText(first_card_data[childPosition]);
        }
        if (groupPosition == 1) {
            textChild.setText(second_card_data[childPosition]);
        }
        if (groupPosition == 2) {
            textChild.setText(third_card_data[childPosition]);
        }


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    static class BankHolder {
        ImageView imgIcon;
        TextView textGroup1;
        TextView textGroup2;
        TextView textGroup3;
        TextView textGroup4;
    }
}